package com.xsimas.xyybackend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xsimas.xyybackend.domain.Carousel;
import com.xsimas.xyybackend.dto.PageRequestDTO;
import com.xsimas.xyybackend.pojo.Result;
import com.xsimas.xyybackend.service.CarouselService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author RAY_HUI
 * @date 2025-07-09
 * @time 17:45
 */

@RestController
@RequestMapping("/api/banner/main")
@Tag(name = "主页面轮播图管理接口", description = "提供轮播图的增删查改及分页操作")
public class MainBannerController {

    @Autowired
    private CarouselService carouselService;

    /**
     * 分页查询轮播图
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询轮播图数据", description = "根据页码和每页数量分页查询轮播图信息")
    public Result<IPage<Carousel>> getCarouselsByPage(@RequestBody PageRequestDTO pageRequest) {
        Page<Carousel> page = new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize());
        return Result.ok(carouselService.page(page));
    }

    /**
     * 查询所有轮播图
     */
    @GetMapping("/list")
    @Operation(summary = "获取所有轮播图列表", description = "返回所有轮播图记录")
    public Result<List<Carousel>> getAllCarousels() {
        return Result.ok(carouselService.list());
    }

    /**
     * 根据ID查询轮播图
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取单个轮播图", description = "通过ID查询轮播图详情")
    public Result<Carousel> getCarouselById(
            @Parameter(description = "轮播图ID", required = true)
            @PathVariable Long id) {
        return Result.ok(carouselService.getById(id));
    }

    /**
     * 新增轮播图
     */
    @PostMapping("/add")
    @Operation(summary = "新增一个轮播图记录", description = "将传入的轮播图对象保存到数据库")
    public Result<Boolean> createCarousel(
            @Parameter(description = "轮播图实体对象", required = true)
            @RequestBody Carousel carousel) {
        boolean success = carouselService.save(carousel);
        return success ? Result.ok(success) : Result.error(400, "新增失败");
    }

    /**
     * 更新轮播图
     */
    @PutMapping("/update")
    @Operation(summary = "更新一个轮播图记录", description = "根据ID更新轮播图信息")
    public Result<Boolean> updateCarousel(
            @Parameter(description = "轮播图实体对象", required = true)
            @RequestBody Carousel carousel) {
        boolean success = carouselService.updateById(carousel);
        return success ? Result.ok(success) : Result.error(400, "更新失败");
    }

    /**
     * 删除轮播图
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "根据ID删除一个轮播图", description = "逻辑或物理删除指定ID的轮播图")
    public Result<Boolean> deleteCarousel(
            @Parameter(description = "轮播图ID", required = true)
            @PathVariable Long id) {
        boolean success = carouselService.removeById(id);
        return success ? Result.ok(success) : Result.error(400, "删除失败");
    }
}